package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.util.UploadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 图片资源控制器
 */
@RestController
@RequestMapping("images")
public class ImagesController {

    @PostMapping
    public Map upload(MultipartFile upload) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            //上传图片
            String imgUrl = UploadUtil.upload(upload, UploadUtil.PATH + "/upload/");
            System.out.println(imgUrl);
            map.put("uploaded", 1);
            map.put("url", imgUrl);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("uploaded", 0);
            HashMap<Object, Object> error = new HashMap<>();
            error.put("message", "上传失败!");
            map.put("error", error);
        }
        return map;
    }

}
